package com.cn.wanxi.io;

import com.cn.wanxi.dto.HomeModelDto;
import com.cn.wanxi.model.HomeModel;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.Common;
import com.sun.jdi.ArrayReference;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 要得到信息
 * 1.将所有信息写入记事本
 * 2.将记事本的信息读取出来
 * 3.封装我们需要的model
 */
public class HomeIO {
    private String path = "D:\\step-three\\home.txt";
    private String[] result;//得到读取出来的值

    /**
     * 1.写入数据
     * 2.读取数据到StringBuilder
     * 3.得到结果集result
     */
    public HomeIO() {
        writeNote();
        String s = Common.readNote(path);
        result = s.split("%%");
    }

    /**
     * 封装主页信息
     *
     * @return
     */
    public HomeModelDto getFromHomeIO() {
        HomeModelDto homeModelDto = new HomeModelDto();
        homeModelDto.setImg(getImg());
        homeModelDto.setTitle(getTitle());
        homeModelDto.setNewsList(getNewsModelList());
        homeModelDto.setProductList(getProductList());
        return homeModelDto;
    }

    /**
     * 取得图片路径
     *
     * @return
     */
    private String getImg() {
        return result[5];
    }

    /**
     * 取得教新动态
     *
     * @return
     */
    private String getTitle() {
        return result[6];
    }

    /**
     * 得到新闻信息
     *
     * @return
     */
    private List<NewsModel> getNewsModelList() {

//        1.写入记事本,写入我们需要字节流
        writeNote();
//        2.读取信息信息
        String result = Common.readNote(path);
//        3.封装到List<NewsModel>
        return getList(result);
    }

    /**
     * 封装list
     *
     * @param result
     * @return
     */
    private List<NewsModel> getList(String result) {
        String[] news = result.split("%%");
        //[日本较大抹茶餐饮七叶和茶登陆上海, 日本较大抹茶餐饮七叶和茶登陆上海, 日本较大抹茶餐饮七叶和茶登陆上海]
        String newsTitle = news[0];//得到新闻标题
        String newsContent = news[1];//得到新闻内容
        newsTitle = Common.replace(newsTitle, "", "[", "]");//将新闻标题左右两边的括号去掉
        newsContent = Common.replace(newsContent, "", "[", "]");

        String[] titles = newsTitle.split(",");
        String[] contents = newsContent.split(",");
        List<NewsModel> list = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            //封装model
            NewsModel newsModel = new NewsModel();
            newsModel.setTitle(titles[i]);
            newsModel.setContent(contents[i]);
            list.add(newsModel);//将所有对象加入list
        }
        return list;
    }


    /**
     * 将新闻信息写入记事本
     */
    private void writeNote() {
        File file = new File(path);
        try {
            OutputStream outputStream = new FileOutputStream(file);//如果我们地址不存在，会进行自动创建
            HomeModel homeModel = new HomeModel();
            //homeModel.newsTitle这个是string数组，但是我们write里面是一个byte数组，所以需要转换
            String[] strings = homeModel.newsTitle;//得到homeModel里面新闻标题是数据
            String newstitle = Arrays.toString(strings);//将数组转换为字符串
            byte[] newstitlebyte = newstitle.getBytes();//将字符串转换为byte数组
            outputStream.write(newstitlebyte);//将byte数组写入记事本
            outputStream.write("%%".getBytes());
            outputStream.write(Arrays.toString(homeModel.newsContent).getBytes());


            outputStream.write("%%".getBytes());
            outputStream.write(Arrays.toString(homeModel.productImgs).getBytes());
            outputStream.write("%%".getBytes());
            outputStream.write(Arrays.toString(homeModel.productNames).getBytes());
            outputStream.write("%%".getBytes());
            outputStream.write(Arrays.toString(homeModel.productContents).getBytes());
            outputStream.write("%%".getBytes());
            outputStream.write(homeModel.getImg().getBytes());//写入新闻图片
            outputStream.write("%%".getBytes());
            outputStream.write(homeModel.title.getBytes());//写入教新动态,


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 得到产品信息
     */
    public List<ProductModel> getProductList() {

        //1.写入记事本
        //2.读取记事本信息
        //3.封装list
        String[] result = Common.readNote(path).split("%%");
        String img = result[2];//这是一个字符串（带中括号）
        String name = result[3];
        String content = result[4];
//        img = Common.replace(img, "[", "");
        img = Common.replace(img, "", "[", "]");
        name = Common.replace(name, "", "[", "]");
        content = Common.replace(content, "", "[", "]");
        String[] imgs = img.split(",");
        String[] names = name.split(",");
        String[] contents = content.split(",");
        List<ProductModel> list = new ArrayList<>();
        for (int i = 0; i < imgs.length; i++) {
            ProductModel productModel = new ProductModel();
            productModel.setImgHref(imgs[i]);
            productModel.setName(names[i]);
            productModel.setContent(contents[i]);
            list.add(productModel);
        }
        return list;
    }

}
